package com.cj.bk_app.util

import android.app.Activity
import com.cj.bk_app.R
import com.cj.bk_app.model.bean.BKResponse
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.coroutineScope
import retrofit2.HttpException


suspend fun executeResponse(response: BKResponse<Any>, successBlock: suspend CoroutineScope.() -> Unit,
                            errorBlock: suspend CoroutineScope.() -> Unit)  {
    coroutineScope {
        if (response.code != 200) errorBlock()
        else successBlock()
    }
}

fun Activity.onNetError(e: Throwable, func: (e: Throwable) -> Unit) {
    if (e is HttpException) {
        toast(R.string.net_error)
        func(e)
    }
}

fun BKResponse<Any>.isSuccess(): Boolean = this.code == 200
